
clear
cd "D:\data_replication"

use estimation\1_data_format\data_base.dta, clear

keep if year < 2006
keep if declarant == 30

// Compute Domestic Share for each product and create index
//----------------------------------------------------------------------------

sort product_id
gen dom_share_temp = exp_share if declarant == partner
by product_id: egen dom_share = mean(dom_share_temp)
by product_id: keep if _n == 1

keep pc8plus dom_share
gen no_dom_pc8plus = 1
replace no_dom_pc8plus = 0 if dom_share != .
gen pc = pc8plus


// Aggregate to 7-digit level
//----------------------------------------------------------------------------

gen ll = length(pc8plus)
gen pc7 = substr(pc8plus, 1, 7) if ll == 8
replace pc7 = pc8plus if ll != 8
//replace pc7 = "2521306" if pc8plus == "2.2004" 
//replace pc7 = "1551203" if pc8plus == "4.2005" 
replace pc7 = "2511157" if pc8plus == "7.2004" 
//replace pc7 = "2722106" if pc8plus == "7.2005" 
//replace pc7 = "1710423" if pc8plus == "10.2005" 
//replace pc7 = "1710425" if pc8plus == "11.2005" 
//replace pc7 = "1824433" if pc8plus == "11.2007" 
//replace pc7 = "1710523" if pc8plus == "12.2005" 
replace pc7 = "2010215" if pc8plus == "12.2007" 
//replace pc7 = "3512115" if pc8plus == "13.2004" 
//replace pc7 = "2123111" if pc8plus == "13.2007" 
//replace pc7 = "1710527" if pc8plus == "14.2005" 
//replace pc7 = "1710529" if pc8plus == "15.2005" 
//replace pc7 = "1710540" if pc8plus == "18.2005" 
replace pc7 = "1720102" if pc8plus == "19.2005" 
//replace pc7 = "1720104" if pc8plus == "21.2005" 
//replace pc7 = "1720108" if pc8plus == "22.2005" 
//replace pc7 = "1720315" if pc8plus == "27.2005" 
replace pc7 = "1720317" if pc8plus == "28.2005" 
//replace pc7 = "1720322" if pc8plus == "30.2005" 
//replace pc7 = "2613121" if pc8plus == "30.2007" 
replace pc7 = "1720323" if pc8plus == "31.2005" 
//replace pc7 = "1720324" if pc8plus == "32.2005" 
replace pc7 = "1720325" if pc8plus == "33.2005" 
//replace pc7 = "1720329" if pc8plus == "34.2005" 
//replace pc7 = "2682119" if pc8plus == "34.2007" 
replace pc7 = "1720333" if pc8plus == "35.2005" 
replace pc7 = "1821112" if pc8plus == "36.2005"
replace pc7 = "1821212" if pc8plus == "40.2005"  
replace pc7 = "1821213" if pc8plus == "41.2005" 
replace pc7 = "3210622" if pc8plus == "41.2007" 
replace pc7 = "1821224" if pc8plus == "42.2005" 
replace pc7 = "3350111" if pc8plus == "42.2007" 
//replace pc7 = "1821225" if pc8plus == "43.2005" 
//replace pc7 = "1822211" if pc8plus == "44.2005" 
//replace pc7 = "3430203" if pc8plus == "44.2007" 
//replace pc7 = "1822221" if pc8plus == "47.2005" 
replace pc7 = "1822222" if pc8plus == "48.2005" 
//replace pc7 = "1822244" if pc8plus == "50.2005" 
replace pc7 = "1822246" if pc8plus == "51.2005" 
replace pc7 = "1822311" if pc8plus == "52.2005" 
//replace pc7 = "1822321" if pc8plus == "55.2005" 
//replace pc7 = "1822322" if pc8plus == "56.2005" 
//replace pc7 = "1822354" if pc8plus == "60.2005"
//replace pc7 = "1823222" if pc8plus == "64.2005"  
replace pc7 = "1823223" if pc8plus == "65.2005" 
replace pc7 = "1823243" if pc8plus == "68.2005" 
//replace pc7 = "1823245" if pc8plus == "69.2005" 
replace pc7 = "1824231" if pc8plus == "75.2005" 
//replace pc7 = "1824235" if pc8plus == "77.2005" 
//replace pc7 = "2420157" if pc8plus == "81.2005" 
//replace pc7 = "2521307" if pc8plus == "83.2005" 
//replace pc7 = "3511213" if pc8plus == "86.2005" 
replace pc7 = "3511223" if pc8plus == "87.2005" 
//replace pc7 = "2722108" if pc8plus == "94.2005" 
//replace pc7 = "2722109" if pc8plus == "95.2005" 
//replace pc7 = "2722109" if pc8plus == "96.2005" 
sort pc7
by pc7: egen dom_share_pc7 = mean(dom_share)
by pc7: egen replace_pc7 = sum(no_dom_pc8plus)
gen no_dom_pc7 = 1
replace no_dom_pc7 = 0 if dom_share_pc7 != .
replace pc = pc7 if replace_pc7 > 0 & dom_share_pc7 != .

// Aggregate to 6-digit level
//----------------------------------------------------------------------------

gen pc6 = substr(pc8plus, 1, 6) if ll == 8
replace pc6 = pc8plus if ll != 8
//replace pc6 = "252130" if pc8plus == "2.2004" 
//replace pc6 = "247023" if pc8plus == "2.2007"
replace pc6 = "193011" if pc8plus == "3.2004"
//replace pc6 = "292212" if pc8plus == "3.2007"
//replace pc6 = "211251" if pc8plus == "4.2004"  
//replace pc6 = "155120" if pc8plus == "4.2005" 
replace pc6 = "251115" if pc8plus == "7.2004"  
//replace pc6 = "272210" if pc8plus == "7.2005" 
//replace pc6 = "272210" if pc8plus == "8.2005" 
//replace pc6 = "171042" if pc8plus == "10.2005" 
//replace pc6 = "171042" if pc8plus == "11.2005" 
//replace pc6 = "182443" if pc8plus == "11.2007" 
replace pc6 = "297124" if pc8plus == "12.2004" 
//replace pc6 = "171052" if pc8plus == "12.2005"
replace pc6 = "201021" if pc8plus == "12.2007"  
//replace pc6 = "351211" if pc8plus == "13.2004" 
//replace pc6 = "212311" if pc8plus == "13.2007" 
//replace pc6 = "171052" if pc8plus == "14.2005" 
//replace pc6 = "212411" if pc8plus == "14.2007" 
//replace pc6 = "171052" if pc8plus == "15.2005" 
//replace pc6 = "271083" if pc8plus == "16.2004" 
replace pc6 = "221412" if pc8plus == "16.2007" 
//replace pc6 = "171054" if pc8plus == "18.2005" 
replace pc6 = "172010" if pc8plus == "19.2005" 
//replace pc6 = "241341" if pc8plus == "19.2007" 
//replace pc6 = "172010" if pc8plus == "21.2005" 
//replace pc6 = "172010" if pc8plus == "22.2005" 
//replace pc6 = "172031" if pc8plus == "27.2005" 
replace pc6 = "172031" if pc8plus == "28.2005" 
//replace pc6 = "245142" if pc8plus == "29.2007" 
//replace pc6 = "172032" if pc8plus == "30.2005"
//replace pc6 = "261312" if pc8plus == "30.2007"  
replace pc6 = "172032" if pc8plus == "31.2005" 
replace pc6 = "261312" if pc8plus == "31.2007" 
//replace pc6 = "172032" if pc8plus == "32.2005" 
replace pc6 = "172032" if pc8plus == "33.2005" 
//replace pc6 = "268211" if pc8plus == "33.2007" 
//replace pc6 = "172032" if pc8plus == "34.2005" 
//replace pc6 = "268211" if pc8plus == "34.2007" 
replace pc6 = "172033" if pc8plus == "35.2005" 
replace pc6 = "182111" if pc8plus == "36.2005" 
replace pc6 = "296014" if pc8plus == "37.2007" 
replace pc6 = "300113" if pc8plus == "39.2007" 
replace pc6 = "182121" if pc8plus == "40.2005"  
//replace pc6 = "300123" if pc8plus == "40.2007" 
replace pc6 = "182121" if pc8plus == "41.2005" 
replace pc6 = "321062" if pc8plus == "41.2007" 
replace pc6 = "182122" if pc8plus == "42.2005" 
replace pc6 = "335011" if pc8plus == "42.2007" 
//replace pc6 = "182122" if pc8plus == "43.2005" 
//replace pc6 = "182221" if pc8plus == "44.2005" 
//replace pc6 = "343020" if pc8plus == "44.2007" 
//replace pc6 = "182221" if pc8plus == "45.2005" 
//replace pc6 = "182221" if pc8plus == "46.2005" 
//replace pc6 = "182222" if pc8plus == "47.2005" 
replace pc6 = "182222" if pc8plus == "48.2005" 
//replace pc6 = "182223" if pc8plus == "49.2005" 
//replace pc6 = "182224" if pc8plus == "50.2005" 
replace pc6 = "182224" if pc8plus == "51.2005" 
replace pc6 = "182231" if pc8plus == "52.2005" 
replace pc6 = "182231" if pc8plus == "53.2005" 
//replace pc6 = "182231" if pc8plus == "54.2005" 
//replace pc6 = "182232" if pc8plus == "55.2005" 
//replace pc6 = "182232" if pc8plus == "56.2005" 
replace pc6 = "266512" if pc8plus == "58.2007" 
//replace pc6 = "182235" if pc8plus == "60.2005" 
replace pc6 = "182235" if pc8plus == "62.2005" 
replace pc6 = "321062" if pc8plus == "62.2007" 
//replace pc6 = "182321" if pc8plus == "63.2005" 
//replace pc6 = "182322" if pc8plus == "64.2005"  
replace pc6 = "182322" if pc8plus == "65.2005" 
replace pc6 = "182324" if pc8plus == "68.2005" 
//replace pc6 = "182324" if pc8plus == "69.2005" 
//replace pc6 = "182413" if pc8plus == "72.2005" 
replace pc6 = "315031" if pc8plus == "74.2007" 
replace pc6 = "182423" if pc8plus == "75.2005" 
//replace pc6 = "182423" if pc8plus == "77.2005" 
//replace pc6 = "193031" if pc8plus == "78.2005" 
//replace pc6 = "242011" if pc8plus == "80.2005" 
//replace pc6 = "242015" if pc8plus == "81.2005" 
//replace pc6 = "252130" if pc8plus == "83.2005" 
//replace pc6 = "261313" if pc8plus == "84.2005" 
//replace pc6 = "294234" if pc8plus == "85.2005" 
//replace pc6 = "351121" if pc8plus == "86.2005" 
replace pc6 = "351122" if pc8plus == "87.2005" 
replace pc6 = "351124" if pc8plus == "88.2005" 
//replace pc6 = "155111" if pc8plus == "91.2005" 
//replace pc6 = "175431" if pc8plus == "92.2005" 
//replace pc6 = "272210" if pc8plus == "94.2005" 
//replace pc6 = "272210" if pc8plus == "95.2005" 
//replace pc6 = "272210" if pc8plus == "96.2005" 
sort pc6
by pc6: egen dom_share_pc6 = mean(dom_share)
by pc6: egen replace_pc6 = sum(no_dom_pc7)
gen no_dom_pc6 = 1
replace no_dom_pc6 = 0 if dom_share_pc6 != .
replace pc = pc6 if replace_pc6 > 0 & dom_share_pc6 != .

// Aggregate to 5-digit level
//----------------------------------------------------------------------------

gen pc5 = substr(pc8plus, 1, 5) if ll == 8
replace pc5 = pc8plus if ll != 8
//replace pc5 = "25213" if pc8plus == "2.2004" 
//replace pc5 = "24702" if pc8plus == "2.2007"
replace pc5 = "19301" if pc8plus == "3.2004"
//replace pc5 = "29221" if pc8plus == "3.2007" 
//replace pc5 = "21125" if pc8plus == "4.2004"  
//replace pc5 = "15512" if pc8plus == "4.2005" 
//replace pc5 = "30021" if pc8plus == "5.2007"
replace pc5 = "25111" if pc8plus == "7.2004" 
//replace pc5 = "27221" if pc8plus == "7.2005" 
replace pc5 = "33403" if pc8plus == "7.2007"
//replace pc5 = "27221" if pc8plus == "8.2005" 
//replace pc5 = "17104" if pc8plus == "10.2005" 
//replace pc5 = "17104" if pc8plus == "11.2005" 
//replace pc5 = "18244" if pc8plus == "11.2007" 
replace pc5 = "29712" if pc8plus == "12.2004" 
//replace pc5 = "17105" if pc8plus == "12.2005" 
replace pc5 = "20102" if pc8plus == "12.2007" 
//replace pc5 = "35121" if pc8plus == "13.2004" 
//replace pc5 = "21231" if pc8plus == "13.2007" 
//replace pc5 = "21121" if pc8plus == "14.2004" 
//replace pc5 = "17105" if pc8plus == "14.2005" 
//replace pc5 = "21241" if pc8plus == "14.2007" 
//replace pc5 = "17105" if pc8plus == "15.2005" 
//replace pc5 = "27108" if pc8plus == "16.2004" 
replace pc5 = "22141" if pc8plus == "16.2007" 
//replace pc5 = "17105" if pc8plus == "18.2005" 
replace pc5 = "17201" if pc8plus == "19.2005"
//replace pc5 = "24134" if pc8plus == "19.2007"  
//replace pc5 = "17201" if pc8plus == "21.2005" 
//replace pc5 = "17201" if pc8plus == "22.2005" 
//replace pc5 = "17203" if pc8plus == "27.2005" 
replace pc5 = "17203" if pc8plus == "28.2005" 
//replace pc5 = "24514" if pc8plus == "29.2007" 
//replace pc5 = "17203" if pc8plus == "30.2005" 
//replace pc5 = "26131" if pc8plus == "30.2007" 
replace pc5 = "17203" if pc8plus == "31.2005" 
replace pc5 = "26131" if pc8plus == "31.2007" 
//replace pc5 = "17203" if pc8plus == "32.2005" 
replace pc5 = "17203" if pc8plus == "33.2005" 
//replace pc5 = "26821" if pc8plus == "33.2007" 
//replace pc5 = "17203" if pc8plus == "34.2005" 
//replace pc5 = "26821" if pc8plus == "34.2007" 
replace pc5 = "17203" if pc8plus == "35.2005" 
replace pc5 = "18211" if pc8plus == "36.2005" 
replace pc5 = "29601" if pc8plus == "37.2007"
replace pc5 = "30011" if pc8plus == "39.2007" 
replace pc5 = "18212" if pc8plus == "40.2005"  
//replace pc5 = "30012" if pc8plus == "40.2007" 
replace pc5 = "18212" if pc8plus == "41.2005" 
replace pc5 = "32106" if pc8plus == "41.2007" 
replace pc5 = "18212" if pc8plus == "42.2005" 
replace pc5 = "33501" if pc8plus == "42.2007" 
//replace pc5 = "18212" if pc8plus == "43.2005"  
//replace pc5 = "18222" if pc8plus == "44.2005" 
//replace pc5 = "34302" if pc8plus == "44.2007" 
//replace pc5 = "18222" if pc8plus == "45.2005" 
//replace pc5 = "35302" if pc8plus == "45.2007" 
//replace pc5 = "18222" if pc8plus == "46.2005" 
//replace pc5 = "18222" if pc8plus == "47.2005" 
replace pc5 = "18222" if pc8plus == "48.2005" 
//replace pc5 = "18222" if pc8plus == "49.2005" 
//replace pc5 = "18222" if pc8plus == "50.2005" 
replace pc5 = "18222" if pc8plus == "51.2005" 
replace pc5 = "18223" if pc8plus == "52.2005" 
replace pc5 = "18223" if pc8plus == "53.2005" 
//replace pc5 = "18223" if pc8plus == "54.2005" 
//replace pc5 = "18223" if pc8plus == "55.2005" 
//replace pc5 = "18223" if pc8plus == "56.2005" 
replace pc5 = "26651" if pc8plus == "58.2007" 
//replace pc5 = "18223" if pc8plus == "60.2005" 
replace pc5 = "18223" if pc8plus == "62.2005" 
replace pc5 = "32106" if pc8plus == "62.2007" 
//replace pc5 = "18232" if pc8plus == "63.2005" 
//replace pc5 = "18232" if pc8plus == "64.2005"  
replace pc5 = "15332" if pc8plus == "64.2007" 
replace pc5 = "18232" if pc8plus == "65.2005" 
replace pc5 = "18232" if pc8plus == "68.2005" 
//replace pc5 = "18232" if pc8plus == "69.2005" 
//replace pc5 = "18241" if pc8plus == "72.2005" 
replace pc5 = "31503" if pc8plus == "74.2007" 
replace pc5 = "18242" if pc8plus == "75.2005" 
//replace pc5 = "18242" if pc8plus == "77.2005" 
//replace pc5 = "19303" if pc8plus == "78.2005" 
//replace pc5 = "24201" if pc8plus == "80.2005" 
//replace pc5 = "24201" if pc8plus == "81.2005" 
//replace pc5 = "25213" if pc8plus == "83.2005" 
//replace pc5 = "26131" if pc8plus == "84.2005" 
//replace pc5 = "29423" if pc8plus == "85.2005" 
//replace pc5 = "35112" if pc8plus == "86.2005" 
replace pc5 = "35112" if pc8plus == "87.2005" 
replace pc5 = "35112" if pc8plus == "88.2005"
//replace pc5 = "15511" if pc8plus == "91.2005" 
//replace pc5 = "17543" if pc8plus == "92.2005" 
//replace pc5 = "27221" if pc8plus == "94.2005" 
//replace pc5 = "27221" if pc8plus == "95.2005"  
//replace pc5 = "27221" if pc8plus == "96.2005" 
//replace pc5 = "14111" if pc5 == "13101" | pc5 == "13201"
sort pc5
by pc5: egen dom_share_pc5 = mean(dom_share)
by pc5: egen replace_pc5 = sum(no_dom_pc6)
gen no_dom_pc5 = 1
replace no_dom_pc5 = 0 if dom_share_pc5 != .
replace pc = pc5 if replace_pc5 > 0 & dom_share_pc5 != .

// Aggregate to 4-digit level
//----------------------------------------------------------------------------

gen pc4 = substr(pc8plus, 1, 4) if ll == 8
replace pc4 = pc8plus if ll != 8
//replace pc4 = "2521" if pc8plus == "2.2004" 
//replace pc4 = "2470" if pc8plus == "2.2007"
replace pc4 = "1930" if pc8plus == "3.2004"
//replace pc4 = "2922" if pc8plus == "3.2007" 
//replace pc4 = "2112" if pc8plus == "4.2004"  
//replace pc4 = "1551" if pc8plus == "4.2005" 
//replace pc4 = "3002" if pc8plus == "5.2007"
replace pc4 = "2511" if pc8plus == "7.2004" 
//replace pc4 = "2722" if pc8plus == "7.2005" 
replace pc4 = "3340" if pc8plus == "7.2007"
//replace pc4 = "2722" if pc8plus == "8.2005" 
//replace pc4 = "1710" if pc8plus == "10.2005" 
//replace pc4 = "1710" if pc8plus == "11.2005" 
//replace pc4 = "1824" if pc8plus == "11.2007" 
replace pc4 = "2971" if pc8plus == "12.2004" 
//replace pc4 = "1710" if pc8plus == "12.2005" 
replace pc4 = "2010" if pc8plus == "12.2007" 
//replace pc4 = "3512" if pc8plus == "13.2004" 
//replace pc4 = "2123" if pc8plus == "13.2007" 
//replace pc4 = "2112" if pc8plus == "14.2004" 
//replace pc4 = "1710" if pc8plus == "14.2005" 
//replace pc4 = "2124" if pc8plus == "14.2007"
//replace pc4 = "1710" if pc8plus == "15.2005"  
//replace pc4 = "2710" if pc8plus == "16.2004" 
replace pc4 = "2214" if pc8plus == "16.2007" 
//replace pc4 = "1710" if pc8plus == "18.2005" 
replace pc4 = "1720" if pc8plus == "19.2005" 
//replace pc4 = "2413" if pc8plus == "19.2007" 
//replace pc4 = "1720" if pc8plus == "21.2005" 
//replace pc4 = "1720" if pc8plus == "22.2005" 
//replace pc4 = "1720" if pc8plus == "27.2005" 
replace pc4 = "1720" if pc8plus == "28.2005" 
//replace pc4 = "2451" if pc8plus == "29.2007" 
//replace pc4 = "1720" if pc8plus == "30.2005" 
//replace pc4 = "2613" if pc8plus == "30.2007" 
replace pc4 = "1720" if pc8plus == "31.2005" 
replace pc4 = "2613" if pc8plus == "31.2007" 
//replace pc4 = "1720" if pc8plus == "32.2005" 
replace pc4 = "1720" if pc8plus == "33.2005" 
//replace pc4 = "2682" if pc8plus == "33.2007" 
//replace pc4 = "1720" if pc8plus == "34.2005" 
//replace pc4 = "2682" if pc8plus == "34.2007" 
replace pc4 = "1720" if pc8plus == "35.2005" 
replace pc4 = "1821" if pc8plus == "36.2005" 
replace pc4 = "2960" if pc8plus == "37.2007" 
replace pc4 = "3001" if pc8plus == "39.2007" 
replace pc4 = "1821" if pc8plus == "40.2005"  
//replace pc4 = "3001" if pc8plus == "40.2007"
replace pc4 = "1821" if pc8plus == "41.2005"  
replace pc4 = "3210" if pc8plus == "41.2007" 
replace pc4 = "1821" if pc8plus == "42.2005" 
replace pc4 = "3350" if pc8plus == "42.2007" 
//replace pc4 = "1821" if pc8plus == "43.2005" 
//replace pc4 = "1822" if pc8plus == "44.2005" 
//replace pc4 = "3430" if pc8plus == "44.2007" 
//replace pc4 = "1822" if pc8plus == "45.2005" 
//replace pc4 = "3530" if pc8plus == "45.2007" 
//replace pc4 = "1822" if pc8plus == "46.2005" 
//replace pc4 = "1822" if pc8plus == "47.2005" 
replace pc4 = "1822" if pc8plus == "48.2005" 
//replace pc4 = "1822" if pc8plus == "49.2005" 
//replace pc4 = "1822" if pc8plus == "50.2005" 
replace pc4 = "1822" if pc8plus == "51.2005" 
replace pc4 = "1822" if pc8plus == "52.2005" 
replace pc4 = "1822" if pc8plus == "53.2005" 
//replace pc4 = "1822" if pc8plus == "54.2005" 
//replace pc4 = "1822" if pc8plus == "55.2005" 
//replace pc4 = "1822" if pc8plus == "56.2005" 
replace pc4 = "2665" if pc8plus == "58.2007" 
//replace pc4 = "1822" if pc8plus == "60.2005" 
replace pc4 = "1822" if pc8plus == "62.2005" 
replace pc4 = "3210" if pc8plus == "62.2007" 
//replace pc4 = "1823" if pc8plus == "63.2005" 
//replace pc4 = "3220" if pc8plus == "63.2007" 
//replace pc4 = "1823" if pc8plus == "64.2005"  
replace pc4 = "1533" if pc8plus == "64.2007" 
replace pc4 = "1823" if pc8plus == "65.2005" 
replace pc4 = "1823" if pc8plus == "68.2005" 
//replace pc4 = "1823" if pc8plus == "69.2005" 
//replace pc4 = "1824" if pc8plus == "72.2005" 
replace pc4 = "3150" if pc8plus == "74.2007"
replace pc4 = "1824" if pc8plus == "75.2005" 
//replace pc4 = "1824" if pc8plus == "77.2005" 
//replace pc4 = "1930" if pc8plus == "78.2005"  
//replace pc4 = "2420" if pc8plus == "80.2005" 
//replace pc4 = "2420" if pc8plus == "81.2005" 
//replace pc4 = "2521" if pc8plus == "83.2005" 
//replace pc4 = "2613" if pc8plus == "84.2005" 
//replace pc4 = "2942" if pc8plus == "85.2005" 
//replace pc4 = "3511" if pc8plus == "86.2005" 
replace pc4 = "3511" if pc8plus == "87.2005" 
replace pc4 = "3511" if pc8plus == "88.2005" 
//replace pc4 = "1551" if pc8plus == "91.2005" 
//replace pc4 = "1754" if pc8plus == "92.2005" 
//replace pc4 = "2722" if pc8plus == "94.2005" 
//replace pc4 = "2722" if pc8plus == "95.2005" 
//replace pc4 = "2722" if pc8plus == "96.2005" 
//replace pc4 = "1411" if pc5 == "14111" 
sort pc4
by pc4: egen dom_share_pc4 = mean(dom_share)
by pc4: egen replace_pc4 = sum(no_dom_pc5)
gen no_dom_pc4 = 1
replace no_dom_pc4 = 0 if dom_share_pc4 != .
replace pc = pc4 if replace_pc4 > 0 & dom_share_pc4 != .

// Aggregate to 3-digit level
//----------------------------------------------------------------------------

gen pc3 = substr(pc8plus, 1, 3) if ll == 8
replace pc3 = pc8plus if ll != 8
//replace pc3 = "252" if pc8plus == "2.2004" 
//replace pc3 = "247" if pc8plus == "2.2007"
replace pc3 = "193" if pc8plus == "3.2004"
//replace pc3 = "292" if pc8plus == "3.2007" 
//replace pc3 = "211" if pc8plus == "4.2004"
//replace pc3 = "155" if pc8plus == "4.2005" 
//replace pc3 = "300" if pc8plus == "5.2007"  
replace pc3 = "251" if pc8plus == "7.2004" 
//replace pc3 = "272" if pc8plus == "7.2005" 
replace pc3 = "334" if pc8plus == "7.2007"
//replace pc3 = "272" if pc8plus == "8.2005" 
//replace pc3 = "171" if pc8plus == "10.2005" 
//replace pc3 = "171" if pc8plus == "11.2005" 
//replace pc3 = "182" if pc8plus == "11.2007" 
replace pc3 = "297" if pc8plus == "12.2004" 
//replace pc3 = "171" if pc8plus == "12.2005" 
replace pc3 = "201" if pc8plus == "12.2007" 
//replace pc3 = "351" if pc8plus == "13.2004" 
//replace pc3 = "212" if pc8plus == "13.2007" 
//replace pc3 = "211" if pc8plus == "14.2004" 
//replace pc3 = "171" if pc8plus == "14.2005" 
//replace pc3 = "212" if pc8plus == "14.2007" 
//replace pc3 = "171" if pc8plus == "15.2005" 
//replace pc3 = "271" if pc8plus == "16.2004" 
replace pc3 = "221" if pc8plus == "16.2007" 
//replace pc3 = "171" if pc8plus == "18.2005" 
replace pc3 = "172" if pc8plus == "19.2005" 
//replace pc3 = "241" if pc8plus == "19.2007" 
//replace pc3 = "172" if pc8plus == "21.2005" 
//replace pc3 = "172" if pc8plus == "22.2005" 
//replace pc3 = "172" if pc8plus == "27.2005" 
replace pc3 = "172" if pc8plus == "28.2005" 
//replace pc3 = "245" if pc8plus == "29.2007" 
//replace pc3 = "172" if pc8plus == "30.2005" 
//replace pc3 = "261" if pc8plus == "30.2007" 
replace pc3 = "172" if pc8plus == "31.2005" 
replace pc3 = "261" if pc8plus == "31.2007" 
//replace pc3 = "172" if pc8plus == "32.2005" 
replace pc3 = "172" if pc8plus == "33.2005" 
//replace pc3 = "268" if pc8plus == "33.2007" 
//replace pc3 = "172" if pc8plus == "34.2005" 
//replace pc3 = "268" if pc8plus == "34.2007" 
replace pc3 = "172" if pc8plus == "35.2005" 
replace pc3 = "182" if pc8plus == "36.2005" 
replace pc3 = "296" if pc8plus == "37.2007" 
replace pc3 = "300" if pc8plus == "39.2007" 
replace pc3 = "182" if pc8plus == "40.2005"  
//replace pc3 = "300" if pc8plus == "40.2007" 
replace pc3 = "182" if pc8plus == "41.2005" 
replace pc3 = "321" if pc8plus == "41.2007" 
replace pc3 = "182" if pc8plus == "42.2005" 
replace pc3 = "335" if pc8plus == "42.2007" 
//replace pc3 = "182" if pc8plus == "43.2005" 
//replace pc3 = "182" if pc8plus == "44.2005" 
//replace pc3 = "343" if pc8plus == "44.2007" 
//replace pc3 = "182" if pc8plus == "45.2005" 
//replace pc3 = "353" if pc8plus == "45.2007" 
//replace pc3 = "182" if pc8plus == "46.2005" 
//replace pc3 = "182" if pc8plus == "47.2005" 
replace pc3 = "182" if pc8plus == "48.2005" 
//replace pc3 = "182" if pc8plus == "49.2005" 
//replace pc3 = "182" if pc8plus == "50.2005" 
replace pc3 = "182" if pc8plus == "51.2005" 
replace pc3 = "182" if pc8plus == "52.2005" 
replace pc3 = "182" if pc8plus == "53.2005" 
//replace pc3 = "182" if pc8plus == "54.2005"
//replace pc3 = "182" if pc8plus == "55.2005"  
//replace pc3 = "182" if pc8plus == "56.2005" 
replace pc3 = "266" if pc8plus == "58.2007" 
//replace pc3 = "182" if pc8plus == "60.2005" 
replace pc3 = "182" if pc8plus == "62.2005" 
replace pc3 = "321" if pc8plus == "62.2007" 
//replace pc3 = "182" if pc8plus == "63.2005" 
//replace pc3 = "322" if pc8plus == "63.2007" 
//replace pc3 = "182" if pc8plus == "64.2005"  
replace pc3 = "153" if pc8plus == "64.2007" 
replace pc3 = "182" if pc8plus == "65.2005" 
replace pc3 = "182" if pc8plus == "68.2005" 
//replace pc3 = "182" if pc8plus == "69.2005" 
//replace pc3 = "182" if pc8plus == "72.2005" 
replace pc3 = "315" if pc8plus == "74.2007" 
replace pc3 = "182" if pc8plus == "75.2005" 
//replace pc3 = "182" if pc8plus == "77.2005" 
//replace pc3 = "193" if pc8plus == "78.2005" 
//replace pc3 = "242" if pc8plus == "80.2005" 
//replace pc3 = "242" if pc8plus == "81.2005" 
//replace pc3 = "252" if pc8plus == "83.2005" 
//replace pc3 = "261" if pc8plus == "84.2005" 
//replace pc3 = "294" if pc8plus == "85.2005" 
//replace pc3 = "351" if pc8plus == "86.2005" 
replace pc3 = "351" if pc8plus == "87.2005" 
replace pc3 = "351" if pc8plus == "88.2005" 
//replace pc3 = "155" if pc8plus == "91.2005" 
//replace pc3 = "175" if pc8plus == "92.2005" 
//replace pc3 = "272" if pc8plus == "94.2005" 
//replace pc3 = "272" if pc8plus == "95.2005" 
//replace pc3 = "272" if pc8plus == "96.2005" 
//replace pc3 = "141" if pc5 == "14111" 
sort pc3
by pc3: egen dom_share_pc3 = mean(dom_share)
by pc3: egen replace_pc3 = sum(no_dom_pc4)
gen no_dom_pc3 = 1
replace no_dom_pc3 = 0 if dom_share_pc3 != .
replace pc = pc3 if replace_pc3 > 0 & dom_share_pc3 != .

// Aggregate to 2-digit level
//----------------------------------------------------------------------------

gen pc2 = substr(pc8plus, 1, 2) if ll == 8
replace pc2 = pc8plus if ll != 8
//replace pc2 = "25" if pc8plus == "2.2004" 
//replace pc2 = "24" if pc8plus == "2.2007"
replace pc2 = "19" if pc8plus == "3.2004"
//replace pc2 = "29" if pc8plus == "3.2007" 
//replace pc2 = "21" if pc8plus == "4.2004" 
//replace pc2 = "15" if pc8plus == "4.2005"  
//replace pc2 = "30" if pc8plus == "5.2007"
replace pc2 = "25" if pc8plus == "7.2004" 
//replace pc2 = "27" if pc8plus == "7.2005" 
replace pc2 = "33" if pc8plus == "7.2007"
//replace pc2 = "27" if pc8plus == "8.2005" 
//replace pc2 = "17" if pc8plus == "10.2005" 
//replace pc2 = "17" if pc8plus == "11.2005" 
//replace pc2 = "18" if pc8plus == "11.2007" 
replace pc2 = "29" if pc8plus == "12.2004" 
//replace pc2 = "17" if pc8plus == "12.2005" 
replace pc2 = "20" if pc8plus == "12.2007" 
//replace pc2 = "35" if pc8plus == "13.2004" 
//replace pc2 = "21" if pc8plus == "13.2007" 
//replace pc2 = "21" if pc8plus == "14.2004" 
//replace pc2 = "17" if pc8plus == "14.2005" 
//replace pc2 = "21" if pc8plus == "14.2007"
//replace pc2 = "17" if pc8plus == "15.2005"  
//replace pc2 = "27" if pc8plus == "16.2004" 
replace pc2 = "22" if pc8plus == "16.2007" 
//replace pc2 = "17" if pc8plus == "18.2005" 
replace pc2 = "17" if pc8plus == "19.2005" 
//replace pc2 = "24" if pc8plus == "19.2007" 
//replace pc2 = "17" if pc8plus == "21.2005" 
//replace pc2 = "17" if pc8plus == "22.2005" 
//replace pc2 = "17" if pc8plus == "27.2005" 
replace pc2 = "17" if pc8plus == "28.2005" 
//replace pc2 = "24" if pc8plus == "29.2007" 
//replace pc2 = "17" if pc8plus == "30.2005" 
//replace pc2 = "26" if pc8plus == "30.2007" 
replace pc2 = "17" if pc8plus == "31.2005" 
replace pc2 = "26" if pc8plus == "31.2007" 
//replace pc2 = "17" if pc8plus == "32.2005" 
replace pc2 = "17" if pc8plus == "33.2005" 
//replace pc2 = "26" if pc8plus == "33.2007" 
//replace pc2 = "17" if pc8plus == "34.2005" 
//replace pc2 = "26" if pc8plus == "34.2007" 
replace pc2 = "17" if pc8plus == "35.2005" 
replace pc2 = "18" if pc8plus == "36.2005" 
replace pc2 = "29" if pc8plus == "37.2007" 
replace pc2 = "30" if pc8plus == "39.2007" 
replace pc2 = "18" if pc8plus == "40.2005"  
//replace pc2 = "30" if pc8plus == "40.2007" 
replace pc2 = "18" if pc8plus == "41.2005" 
replace pc2 = "32" if pc8plus == "41.2007" 
replace pc2 = "18" if pc8plus == "42.2005" 
replace pc2 = "33" if pc8plus == "42.2007" 
//replace pc2 = "18" if pc8plus == "43.2005" 
//replace pc2 = "18" if pc8plus == "44.2005" 
//replace pc2 = "34" if pc8plus == "44.2007" 
//replace pc2 = "18" if pc8plus == "45.2005" 
//replace pc2 = "35" if pc8plus == "45.2007" 
//replace pc2 = "18" if pc8plus == "46.2005" 
//replace pc2 = "18" if pc8plus == "47.2005" 
replace pc2 = "18" if pc8plus == "48.2005" 
//replace pc2 = "18" if pc8plus == "49.2005" 
//replace pc2 = "18" if pc8plus == "50.2005" 
replace pc2 = "18" if pc8plus == "51.2005" 
replace pc2 = "18" if pc8plus == "52.2005" 
replace pc2 = "18" if pc8plus == "53.2005" 
//replace pc2 = "18" if pc8plus == "54.2005" 
//replace pc2 = "18" if pc8plus == "55.2005" 
//replace pc2 = "18" if pc8plus == "56.2005" 
replace pc2 = "26" if pc8plus == "58.2007" 
//replace pc2 = "18" if pc8plus == "60.2005" 
replace pc2 = "18" if pc8plus == "62.2005" 
replace pc2 = "32" if pc8plus == "62.2007" 
//replace pc2 = "18" if pc8plus == "63.2005" 
//replace pc2 = "32" if pc8plus == "63.2007" 
//replace pc2 = "18" if pc8plus == "64.2005"  
replace pc2 = "15" if pc8plus == "64.2007" 
replace pc2 = "18" if pc8plus == "65.2005" 
replace pc2 = "18" if pc8plus == "68.2005" 
//replace pc2 = "18" if pc8plus == "69.2005" 
//replace pc2 = "18" if pc8plus == "72.2005" 
replace pc2 = "31" if pc8plus == "74.2007" 
replace pc2 = "18" if pc8plus == "75.2005" 
//replace pc2 = "18" if pc8plus == "77.2005" 
//replace pc2 = "19" if pc8plus == "78.2005" 
//replace pc2 = "24" if pc8plus == "80.2005" 
//replace pc2 = "24" if pc8plus == "81.2005" 
//replace pc2 = "25" if pc8plus == "83.2005" 
//replace pc2 = "26" if pc8plus == "84.2005" 
//replace pc2 = "29" if pc8plus == "85.2005" 
//replace pc2 = "35" if pc8plus == "86.2005" 
replace pc2 = "35" if pc8plus == "87.2005" 
replace pc2 = "35" if pc8plus == "88.2005" 
//replace pc2 = "15" if pc8plus == "91.2005" 
//replace pc2 = "17" if pc8plus == "92.2005" 
//replace pc2 = "27" if pc8plus == "94.2005" 
//replace pc2 = "27" if pc8plus == "95.2005"
//replace pc2 = "27" if pc8plus == "96.2005"  
//replace pc2 = "14" if pc5 == "14111" 
sort pc2
by pc2: egen dom_share_pc2 = mean(dom_share)
by pc2: egen replace_pc2 = sum(no_dom_pc3)
gen no_dom_pc2 = 1
replace no_dom_pc2 = 0 if dom_share_pc2 != .
replace pc = pc2 if replace_pc2 > 0 & dom_share_pc2 != .

save estimation\1_product_list\output\A30_data_sweden_temp.dta, replace


// Summarize composite categories
//------------------------------------------------------------------------------

sort pc
gen pc8plus_d = 0
replace pc8plus_d = 1 if ll != 8 & pc8plus != pc
sort pc
by pc: egen pc8plus_comp = max(pc8plus_d)

keep if pc8plus_comp == 1
keep if ll != 8
keep pc pc8plus
order pc pc8plus
save estimation\1_product_list\output\A30_pc_comp_sweden.dta, replace


// Summarize non-composite categories
//------------------------------------------------------------------------------

use estimation\1_product_list\output\A30_data_sweden_temp.dta, replace
sort pc
gen pc8plus_d = 0
replace pc8plus_d = 1 if ll != 8 & pc8plus != pc
sort pc
by pc: egen pc8plus_comp = max(pc8plus_d)

drop if pc8plus_comp == 1
sort pc
by pc: keep if _n == 1
keep pc 
save estimation\1_product_list\output\A30_pc_sweden.dta, replace

rm estimation\1_product_list\output\A30_data_sweden_temp.dta


